/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BD.Seccion;
import BD.Usuario;
import DigaFoil.Controlador;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ARMGARCES
 */
public class PantallaPpal extends javax.swing.JFrame implements Pantalla{
    private boolean sesion;
    private String imagen = java.util.ResourceBundle.getBundle("GUI/fondos").getString("fondoPpal");
    /**
     * Creates new form PantallaPpal
     */
    public PantallaPpal() {        
        initComponents();
        setTitle("DIGA C.A. - DIGAFOIL #1");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        this.setSize(screenSize.width,screenSize.height);

        int x = 0;
        int y = 0;
        this.setLocation(x, y);        
        b_admin.setEnabled(false);
        b_proveedor.setEnabled(false);
        sesion = true;
    }

    /**
     * 
     * @param _user
     */
    @Override
    public void updateSesion(Usuario _user){
        if(_user != null && _user.getActivo()){
            l_sesion.setText(_user.toString());
            b_sesion.setText("Cerrar Sesion");
            sesion = false;
            habilitarSecciones();
        }else{
            l_sesion.setText("Usuario sin Sesion Iniciada");
            b_sesion.setText("Iniciar Sesion");
            sesion = true; 
            deshabilitarSecciones();
        }
        panelSesion.repaint();
           
    }
    /**
     * 
     */
    @Override
    public void habilitarSecciones(){
        
        List<Seccion> secciones = new ArrayList<Seccion>();
        Usuario usuario = Controlador.getInstance().getUser();
        secciones = usuario.getFkidRol().getSeccionesHabilitadas();     
        // SECCIONES A SER HABILITADAS SEGUN EL ROL DE USUARIO
        
        for(Seccion seccion : secciones){
            //SECCION ADMIN
            if(seccion.getNombre().equals("Admin")) {
                this.b_admin.setEnabled(true);
            }
            //SECCION DE REPORTES
            if(seccion.getNombre().equals("Proveedores")){
                this.b_proveedor.setEnabled(true);
            }
        }
        
    }
    /**
     * 
     */
    @Override
    public void deshabilitarSecciones(){
        this.b_admin.setEnabled(false);
        this.b_proveedor.setEnabled(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fondo = new JPanelConFondo(imagen);
        panelSesion = new javax.swing.JPanel();
        b_sesion = new javax.swing.JButton();
        l_sesion = new javax.swing.JLabel();
        b_admin = new javax.swing.JButton();
        b_proveedor = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        fondo.setMinimumSize(new java.awt.Dimension(800, 600));
        fondo.setPreferredSize(new java.awt.Dimension(800, 600));

        panelSesion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sesion", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 10))); // NOI18N

        b_sesion.setText("Iniciar Sesion");
        b_sesion.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        b_sesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_sesionActionPerformed(evt);
            }
        });

        l_sesion.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        l_sesion.setText("Usuario sin Sesion Iniciada  ");

        javax.swing.GroupLayout panelSesionLayout = new javax.swing.GroupLayout(panelSesion);
        panelSesion.setLayout(panelSesionLayout);
        panelSesionLayout.setHorizontalGroup(
            panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSesionLayout.createSequentialGroup()
                .addGroup(panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelSesionLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(l_sesion, javax.swing.GroupLayout.DEFAULT_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelSesionLayout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(b_sesion)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelSesionLayout.setVerticalGroup(
            panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSesionLayout.createSequentialGroup()
                .addComponent(l_sesion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(b_sesion)
                .addContainerGap())
        );

        b_sesion.getAccessibleContext().setAccessibleDescription("");

        b_admin.setText("Configuraciones");
        b_admin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_adminActionPerformed(evt);
            }
        });

        b_proveedor.setText("Proveedores");
        b_proveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_proveedorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout fondoLayout = new javax.swing.GroupLayout(fondo);
        fondo.setLayout(fondoLayout);
        fondoLayout.setHorizontalGroup(
            fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoLayout.createSequentialGroup()
                .addGroup(fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fondoLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panelSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(fondoLayout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(b_proveedor)
                        .addGap(0, 518, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fondoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(b_admin)
                .addGap(54, 54, 54))
        );
        fondoLayout.setVerticalGroup(
            fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(110, 110, 110)
                .addComponent(b_proveedor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE)
                .addComponent(b_admin)
                .addGap(229, 229, 229))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondo, javax.swing.GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondo, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_sesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_sesionActionPerformed
        // TODO add your handling code here:
        //
        if(sesion){
            Gui.getInstance().iniciarSesion();
        }else{
            Controlador.getInstance().cerrarSesion();           
        }
    }//GEN-LAST:event_b_sesionActionPerformed

    private void b_adminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_adminActionPerformed
        // TODO add your handling code here:
        //
        Gui.getInstance().abrirPantallaAdmin(Controlador.getInstance().getUser());
       
    }//GEN-LAST:event_b_adminActionPerformed

    private void b_proveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_proveedorActionPerformed
        // TODO add your handling code here:
        Gui.getInstance().abrirPantallaProveedores(Controlador.getInstance().getUser());
    }//GEN-LAST:event_b_proveedorActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b_admin;
    private javax.swing.JButton b_proveedor;
    private javax.swing.JButton b_sesion;
    private javax.swing.JPanel fondo;
    private javax.swing.JLabel l_sesion;
    private javax.swing.JPanel panelSesion;
    // End of variables declaration//GEN-END:variables

    @Override
    public void salir() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
